package controller

import (
	"gin_server/internal/request" // 请替换为实际的包路径
	"gin_server/internal/service" // 请替换为实际的包路径
	"gin_server/pkg/response"     // 请替换为实际的包路径
	"strconv"                     // 请替换为实际的包路径

	"github.com/gin-gonic/gin"
)

type GroupController struct {
	service service.GroupService
}

func NewGroupController(s service.GroupService) *GroupController {
	return &GroupController{service: s}
}

// GetGroup 根据ID获取群组
// @Summary 根据ID获取群组
// @Description 根据ID获取群组信息
// @Tags 群组
// @Accept json
// @Produce json
// @Param id path string true "groupID"
// @Success 200 {object} response.Response{data=model.Group}
// @Security BearerAuth
// @Router /group/{id} [get]
func (c *GroupController) GetGroup(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	entity, err := c.service.GetGroupByID(uint(id))
	if err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "获取成功", entity)
}

// GetGroupList 获取群组列表
// @Summary 获取群组列表
// @Description 获取群组列表
// @Tags 群组
// @Accept json
// @Produce json
// @Param params query request.GroupQueryRequest true "群组查询参数"
// @Success 200 {object} response.Response{data=response.PageResponse{total=int64,list=[]model.Group}}
// @Security BearerAuth
// @Router /group [get]
func (c *GroupController) GetGroupList(ctx *gin.Context) {
	var query request.GroupQueryRequest
	// 绑定查询参数
	if err := ctx.ShouldBindQuery(&query); err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	entities, total, err := c.service.GetAllGroup(&query)
	if err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.PageSuccess(ctx, total, entities)
}

// CreateGroup 创建群组
// @Summary 创建新群组
// @Description 创建新群组记录
// @Tags 群组
// @Accept json
// @Produce json
// @Param entity body request.GroupCreateRequest true "群组信息"
// @Success 200 {object} response.Response{data=model.Group}
// @Security BearerAuth
// @Router /group [post]
func (c *GroupController) CreateGroup(ctx *gin.Context) {
	var entity request.GroupCreateRequest
	if err := ctx.ShouldBindJSON(&entity); err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	result, createErr := c.service.CreateGroup(entity.ToModel())
	if createErr != nil {
		response.Error(ctx, createErr.Error())
		return
	}

	response.SuccessWithMessage(ctx, "创建成功", result)
}

// UpdateGroup 更新群组
// @Summary 更新群组
// @Description 更新群组信息
// @Tags 群组
// @Accept json
// @Produce json
// @Param id path string true "groupID"
// @Param entity body request.GroupUpdateRequest true "群组信息"
// @Success 200 {object} response.Response{data=model.Group}
// @Security BearerAuth
// @Router /group/{id} [put]
func (c *GroupController) UpdateGroup(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	var entity request.GroupUpdateRequest
	if err := ctx.ShouldBindJSON(&entity); err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	if err := c.service.UpdateGroup(uint(id), &entity); err != nil {
		response.Error(ctx, err.Error())
		return
	}

	result, getErr := c.service.GetGroupByID(uint(id))
	if getErr != nil {
		response.Error(ctx, getErr.Error())
		return
	}

	response.SuccessWithMessage(ctx, "更新成功", result)
}

// DeleteGroup 删除群组
// @Summary 删除群组
// @Description 删除群组
// @Tags 群组
// @Accept json
// @Produce json
// @Param id path string true "groupID"
// @Success 200 {object} response.Response{data=nil}
// @Security BearerAuth
// @Router /group/{id} [delete]
func (c *GroupController) DeleteGroup(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	if err := c.service.DeleteGroup(uint(id)); err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "删除成功", nil)
}

// RestoreGroup 恢复群组
// @Summary 恢复群组
// @Description 恢复群组
// @Tags 群组
// @Accept json
// @Produce json
// @Param id path string true "groupID"
// @Success 200 {object} response.Response{data=nil}
// @Security BearerAuth
// @Router /group/{id}/restore [put]
func (c *GroupController) RestoreGroup(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	if err := c.service.RestoreGroup(uint(id)); err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "恢复成功", nil)
}

// ForceDeleteGroup 真删除群组
// @Summary 真删除群组
// @Description 真删除群组
// @Tags 群组
// @Accept json
// @Produce json
// @Param id path string true "groupID"
// @Success 200 {object} response.Response{data=nil}
// @Security BearerAuth
// @Router /group/{id}/force [delete]
func (c *GroupController) ForceDeleteGroup(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	if err := c.service.ForceDeleteGroup(uint(id)); err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "删除成功", nil)
}
